home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13628 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP: Overloading [] operator for multidimensional array of objects.
  5. Date: Tue, 26 Mar 1996 12:01:21 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <315822E1.7B07@datalytics.com>
  8. References: <4j6p2v$dhg@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Edgar L. Torres wrote:
  16. > Hi!
  17. >         I would like to specify the overload of [], such that I may access
  18. > a multidimensional array of objects. As an example, given I have a
  19. > class
  20. > EXAMCLASS and I wish to dynamically allocate a three-dimensional array
  21. > of
  22. > objects:
  23. >         EXAMCLASS object1***;
  24. >         ...
  25. >         object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
  26. > I know the above is sacrilege to C++ purists, but bare with me, since
  27. > the malloc
  28. > does work correctly in C++.  Now, what I want is to be able to:
  29. >         object1[arg1][arg2][arg3].obj1Var1 = something;
  30. > OR
  31. >         something = object1[arg1][arg2][arg3].obj1Var1;
  32. > In general, I know I have to include three friend operator in the
  33. > definition of
  34. > class EXAMCLASS, and then define them outside:
  35. >         EXAMCLASS** operator[](EXAMCLASS*** C, int i){
  36. >                 return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
  37. >         EXAMCLASS* operator[](EXAMCLASS** C, int i){
  38. >                 return ((EXAMCLASS *)(C + (i * dim3sz))); }
  39. >         EXAMCLASS operator[](EXAMCLASS* C, int i){
  40. >                 return *(C + i); }
  41. > I know that the above does not work, but it does present the basic
  42. > idea of what
  43. > I am trying to do.  If anybody has the knowledge to help with the
  44. > specifics of
  45. > syntax and argument rules, please HELP ME!  Somebody suggested
  46. > replacing the *
  47. > with &, but I am not sure what EXAMCLASS&& means??
  48. > Thanks in advance,
  49. > Edgar
  50.  
  51. You've tried to redefine operator [], which you cannot do.  It 
  52. takes only a single dimension.  The juxtaposition of multiple 
  53. []'s invokes operator [] on different types.  That is, given the 
  54. declaration:
  55.  
  56. EXAMCLASS*** p;
  57.  
  58. then,
  59.  
  60. p[a]
  61.  
  62. refers to the a'th EXAMCLASS** in p.  And,
  63.  
  64. p[a][b]
  65.  
  66. refers to the b'th EXAMCLASS* in the a'th EXAMCLASS** in p.  
  67. And,
  68.  
  69. p[a][b][c]
  70.  
  71. refers to the c'th EXAMCLASS in the b'th EXAMCLASS* in the a'th 
  72. EXAMCLASS** in p.  Whew!
  73.  
  74. The point is, you're not really looking at a multidimensional 
  75. array.  You're accessing it as one, but it isn't one.  The 
  76. solution is to use an array/vector template class parameterized 
  77. on EXAMCLASS, then a/v<EXAMCLASS>, then a/v<a/v<EXAMCLASS>>.  In 
  78. other words, do the following (using Rogue Wave's RWTVector as 
  79. an example vector template class):
  80.  
  81. RWVector<RWVector<RWVector<EXAMCLASS>>> object1;
  82.  
  83. Now you can use up to three []'s to access the various objects 
  84. in object1.
  85.  
  86. -- 
  87. Robert Stewart        | My opinions are usually my own.
  88. Datalytics, Inc.    | stew@datalytics.com
  89.